package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.StudentOtherServiceInt;
import yx.vo.DatagridVO;
import yx.vo.HobbyVO;
import yx.vo.JsonVO;
import yx.vo.StudentOtherVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "studentOtherAction")
public class StudentOtherAction extends BaseAction implements ModelDriven<StudentOtherVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StudentOtherAction.class);

	private StudentOtherVO studentOther = new StudentOtherVO();

	@Override
	public StudentOtherVO getModel() {
		return studentOther;
	}

	private StudentOtherServiceInt studentOtherService;

	public StudentOtherServiceInt getStudentOtherService() {
		return studentOtherService;
	}

	@Autowired
	public void setStudentOtherService(StudentOtherServiceInt studentOtherService) {
		this.studentOtherService = studentOtherService;
	}

	public void queryStudentOther() {
		if (studentOther.getStudentId() != null) {
			DatagridVO datagridVO = studentOtherService.datagrid(studentOther);
			if(datagridVO.getTotal().intValue() != 0){
				StudentOtherVO other = (StudentOtherVO)datagridVO.getRows().get(0);
				super.writeJson(other);
			} else {
				JsonVO j = new JsonVO();
				j.setSuccess(true);
				j.setMsg("学生相关信息不存在！");
				super.writeJson(j);
			}
		} else {
			JsonVO j = new JsonVO();
			j.setSuccess(false);
			j.setMsg("学生主键丢失！");
			super.writeJson(j);
		}
	}

	public void addStudentOther() {
		JsonVO j = new JsonVO();
		try {
			StudentOtherVO u = studentOtherService.save(studentOther);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeStudentOther() {
		studentOtherService.remove(studentOther.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editStudentOther() {
		JsonVO j = new JsonVO();
		try {
			StudentOtherVO u = studentOtherService.edit(studentOther);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void addOrEditStudentOther() {
		JsonVO j = new JsonVO();
		try {
			if (studentOther.getId() == null) {
				StudentOtherVO u = studentOtherService.save(studentOther);
				j.setSuccess(true);
				j.setMsg("增加成功！");
				j.setObj(u);
			} else {
				StudentOtherVO u = studentOtherService.edit(studentOther);
				j.setSuccess(true);
				j.setMsg("编辑成功！");
				j.setObj(u);
			}
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
}
